package cn.edu.hpu.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.hpu.dto.DealnoteDto;
import cn.edu.hpu.pojo.Deal;
import cn.edu.hpu.pojo.Dealnote;
import cn.edu.hpu.pojo.User;
import cn.edu.hpu.service.DealService;
import cn.edu.hpu.service.DealnoteService;
import cn.edu.hpu.service.UserService;
import cn.edu.hpu.util.DateUtil;
import cn.edu.hpu.util.UUIDUtil;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class DealnoteAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private DealnoteService dealnoteService;
	private String did;
	private String uid;
	private Dealnote dealnote;
	private UserService userService;
	private DealService dealService;
	private String id;
	private String content;
	private Dealnote dnote;
	
	public String  execute(){
		return "success";
	}


	public String save() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		
		dnote = new Dealnote();
		
		System.out.println(did);
		System.out.println(content);
		User user = userService.findAUser(uid);
		Deal deal = dealService.findById(did);
		
		dnote.setUser(user);
		dnote.setDeal(deal);
		dnote.setId(UUIDUtil.getUUID());
		dnote.setCreatetime(DateUtil.getTime());
		dnote.setContent(content);
		dnote.setState("good");
		dealnoteService.save(dnote);
		
		DealnoteDto dto = new DealnoteDto();
		dto.setContent(dnote.getContent());
		dto.setCreatetime(dnote.getCreatetime());
		dto.setId(dnote.getId());
		Gson g = new Gson();
		String json = g.toJson(dto);
		
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(json);
		out.flush();
		out.close();
		return "success";
	}
	
	public String del(){
		System.out.println(id);
		Dealnote dealnote = dealnoteService.findById(id);
		did = dealnote.getDeal().getId();
		dealnoteService.delete(id);
		return "success";
	}
	public String update() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		Dealnote dnote = dealnoteService.findById(id);
		DealnoteDto dto = new DealnoteDto();
		dto.setContent(dnote.getContent());
		dto.setCreatetime(dnote.getCreatetime());
		dto.setId(dnote.getId());
		Gson g = new Gson();
		String json = g.toJson(dto);
		
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(json);
		out.flush();
		out.close();
		return "success";
	}
	
	public String edit() throws IOException{
		
		HttpServletResponse response = ServletActionContext.getResponse();
		 Dealnote dealnote = dealnoteService.findById(id);
		
		dealnote.setCreatetime(DateUtil.getTime());
		dealnote.setContent(content);
		
		dealnoteService.update(dealnote);
		
		DealnoteDto dto = new DealnoteDto();
		dto.setContent(dealnote.getContent());
		dto.setCreatetime(dealnote.getCreatetime());
		dto.setId(dealnote.getId());
		Gson g = new Gson();
		String json = g.toJson(dto);
		
		System.out.println(json);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(json);
		out.flush();
		out.close();
		
		return "success";
	}
	//set get
	public String getDid() {
		return did;
	}

	public void setDid(String did) {
		this.did = did;
	}

	public DealnoteService getDealnoteService() {
		return dealnoteService;
	}
	@Autowired
	public void setDealnoteService(DealnoteService dealnoteService) {
		this.dealnoteService = dealnoteService;
	}


	public String getUid() {
		return uid;
	}


	public void setUid(String uid) {
		this.uid = uid;
	}


	public Dealnote getDealnote() {
		return dealnote;
	}


	public void setDealnote(Dealnote dealnote) {
		this.dealnote = dealnote;
	}


	public UserService getUserService() {
		return userService;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public DealService getDealService() {
		return dealService;
	}


	public void setDealService(DealService dealService) {
		this.dealService = dealService;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}


	public Dealnote getDnote() {
		return dnote;
	}


	public void setDnote(Dealnote dnote) {
		this.dnote = dnote;
	}
	
	
	
}

